package sjtu.ist.warehouse4ws.action;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;

import sjtu.ist.warehouse4ws.model.Column;
import sjtu.ist.warehouse4ws.model.CustColumn;
import sjtu.ist.warehouse4ws.service.ColumnService;
import sjtu.ist.warehouse4ws.service.cust.CustColumnService;

import com.opensymphony.xwork2.ActionSupport;

public class ColumnAction extends ActionSupport {
	//service
	CustColumnService custColumnService;
	ColumnService columnService;
	
	//output
	List<Column> columnList=new ArrayList<Column>();
	
	//input
	String tablename;
	String columnname;
	int type;
	
	public String showTableColumn(){
		Subject currentUser = SecurityUtils.getSubject();
		
		if(!currentUser.hasRole("superadmin")){
			columnList=custColumnService.getByTable("cust_"+tablename);
		}else{
			columnList=columnService.getByTable(tablename);
		}
		return SUCCESS;
	}
	
	public String addTableColumn(){
		Subject currentUser = SecurityUtils.getSubject();
		
		if(!currentUser.hasRole("superadmin")){	
			CustColumn cc=new CustColumn();
			cc.setColumnname(columnname);
			cc.setTablename("cust_"+tablename);
			cc.setType(type);
			custColumnService.addColumn(cc);
		}else{
			Column cc=new Column();
			cc.setColumnname(columnname);
			cc.setTablename(tablename);
			cc.setType(type);
			columnService.addColumn(cc);
		}
		return SUCCESS;
	}
	
	@JSON(name = "column")
	public List<Column> getColumnList() {
		return columnList;
	}

	@JSON(name = "total")
	public int getTotalColumnList() {
		return columnList.size();
	}

	public void setCustColumnService(CustColumnService custColumnService) {
		this.custColumnService = custColumnService;
	}

	public void setColumnService(ColumnService columnService) {
		this.columnService = columnService;
	}

	public void setTablename(String tablename) {
		this.tablename = tablename;
	}

	public void setColumnname(String columnname) {
		this.columnname = columnname;
	}

	public void setType(int type) {
		this.type = type;
	}
}